bitkeeper revision 1.1724 (42b7d6f60v0z2ZEkw36W_1joFfySfw)
authorcl349@firebug.cl.cam.ac.uk <cl349@firebug.cl.cam.ac.uk>
Tue, 21 Jun 2005 08:59:34 +0000 (08:59 +0000)
committercl349@firebug.cl.cam.ac.uk <cl349@firebug.cl.cam.ac.uk>
Tue, 21 Jun 2005 08:59:34 +0000 (08:59 +0000)
Make xs_read/read_reply allocate an extra byte and put a nul character
at the end of objects to allow easy use as a string.
From: Rusty Russell <rusty@rustcorp.com.au>
Signed-off-by: Christian Limpach <Christian.Limpach@cl.cam.ac.uk>
tools/xenstore/xs.c
tools/xenstore/xs.h
tools/xenstore/xs_test.c

index e41ca652bde226dc605688127f53b65314c001a1..c11e02ae1e210bfcbc22dfe18a683bbd7a4be918 100644 (file)
@@ -131,6 +131,7 @@ static int get_error(const char *errorstring)
        return xsd_errors[i].errnum;
 }
 
+/* Adds extra nul terminator, because we generally (always?) hold strings. */
 static void *read_reply(int fd, enum xsd_sockmsg_type *type, unsigned int *len)
 {
        struct xsd_sockmsg msg;
@@ -140,7 +141,7 @@ static void *read_reply(int fd, enum xsd_sockmsg_type *type, unsigned int *len)
        if (!read_all(fd, &msg, sizeof(msg)))
                return NULL;
 
-       ret = malloc(msg.len);
+       ret = malloc(msg.len + 1);
        if (!ret)
                return NULL;
 
@@ -154,6 +155,7 @@ static void *read_reply(int fd, enum xsd_sockmsg_type *type, unsigned int *len)
        *type = msg.type;
        if (len)
                *len = msg.len;
+       ((char *)ret)[msg.len] = '\0';
        return ret;
 }
 
@@ -269,9 +271,9 @@ char **xs_directory(struct xs_handle *h, const char *path, unsigned int *num)
        return ret;
 }
 
-/* Get the value of a single file.
+/* Get the value of a single file, nul terminated.
  * Returns a malloced value: call free() on it after use.
- * len indicates length in bytes.
+ * len indicates length in bytes, not including the nul.
  */
 void *xs_read(struct xs_handle *h, const char *path, unsigned int *len)
 {
index b778cedd654feecd01128e2423ccfd17245e9f73..09d5a20937740db0311b82203d4164af134c8b35 100644 (file)
@@ -45,9 +45,9 @@ void xs_daemon_close(struct xs_handle *);
  */
 char **xs_directory(struct xs_handle *h, const char *path, unsigned int *num);
 
-/* Get the value of a single file.
+/* Get the value of a single file, nul terminated.
  * Returns a malloced value: call free() on it after use.
- * len indicates length in bytes.
+ * len indicates length in bytes, not including the nul.
  */
 void *xs_read(struct xs_handle *h, const char *path, unsigned int *len);
 
index 29929b769363b4654b24734f4599dcb899de3cb6..6ce5d701af5a354d3e921944ab9c8a81ee1afa27 100644 (file)
@@ -240,6 +240,8 @@ static void do_read(unsigned int handle, char *path)
        if (!value)
                failed(handle);
 
+       /* It's supposed to nul terminate for us. */
+       assert(value[len] == '\0');
        if (handle)
                printf("%i:%.*s\n", handle, len, value);
        else
@@ -261,7 +263,7 @@ static void do_write(unsigned int handle, char *path, char *flags, char *data)
        else
                barf("write flags 'none', 'create' or 'excl' only");
 
-       if (!xs_write(handles[handle], path, data, strlen(data)+1, f))
+       if (!xs_write(handles[handle], path, data, strlen(data), f))
                failed(handle);
 }